import 'package:flutter/material.dart';


import '../../resources/kq_theme_colors.dart';

class KqIconTagView extends StatelessWidget {
  ///边框间距
  final EdgeInsetsGeometry? padding;

  ///总宽度
  final double? width;

  ///总高度
  final double? height;

  ///文本超出后样式,默认为超出省略末尾
  final TextOverflow? overflow;

  ///最大行高
  final int maxLines;

  ///文案
  final String text;

  ///竖直对齐方式
  final CrossAxisAlignment crossAxisAlignment;

  ///水平对齐布局
  final MainAxisAlignment mainAxisAlignment;

  ///文案大小默认14
  final double? fontSize;

  ///背景色
  final Color? backgroundColor;

  ///文案颜色
  final Color? textColor;

  ///icon地址
  final String iconPath;

  ///icon宽默认15,viewType=3时，改宽为左边问的最大宽度
  final double? iconWidth;

  ///icon高默认等于宽
  final double? iconHeight;

  ///icon与text之间的间距。默认6
  final double? spacing;

  ///样式，1:图左文右，2:图右文左，3:文本加文本格式，左边文本的宽高使用icon的宽高，其中宽为最大宽度默认为30
  final int? viewType;

  ///左边文本文案
  final String? leftText;

  ///左边文本文字大小
  final double? leftFont;

  ///左边文本文字颜色
  final Color? leftTextColor;

  const KqIconTagView({
    Key? key,
    this.padding,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.width,
    this.height,
    this.overflow = TextOverflow.ellipsis,
    this.maxLines = 1,
    required this.text,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.fontSize = 0,
    this.backgroundColor = KqThemeColors.bgWhite,
    this.textColor = KqThemeColors.text59,
    required this.iconPath,
    this.iconWidth,
    this.iconHeight,
    this.spacing,
    this.viewType = 1,
    this.leftText,
    this.leftFont,
    this.leftTextColor,
  }) : super(key: key);

  List<Widget> getTagView() {
    List<Widget> view = [];
    Text countText = Text(
      text,
      maxLines: maxLines,
      overflow: overflow,
      style: TextStyle(
        color: textColor,
        fontSize: fontSize == 0 ? 14 : fontSize,
      ),
    );
    Image icon = Image.asset(
      iconPath,
      fit: BoxFit.contain,
      height: iconHeight ?? iconWidth ?? 15,
      width: iconWidth ?? 15,
    );

    ConstrainedBox leftTextView = ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: iconWidth ?? 30,
      ),
      child: SizedBox(
        height: iconHeight,
        child: Text(
          leftText ?? '',
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
              fontSize: leftFont ?? 14,
              color: leftTextColor ?? KqThemeColors.text26),
        ),
      ),
    );
    Padding iconP = Padding(
      padding: viewType == 1 || viewType == 3
          ? EdgeInsets.only(right: spacing ?? 6)
          : EdgeInsets.only(left: spacing ?? 6),
      child: viewType == 3 ? leftTextView : icon,
    );
    if (viewType == 1 || viewType == 3) {
      view.addAll(
          [iconP, width == null ? countText : Expanded(child: countText)]);
    } else if (viewType == 2) {
      view.addAll(
          [width == null ? countText : Expanded(child: countText), iconP]);
    }
    return view;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding,
      width: width,
      height: height,
      color: backgroundColor,
      child: Row(
        crossAxisAlignment: crossAxisAlignment,
        mainAxisAlignment: mainAxisAlignment,
        children: getTagView(),
      ),
    );
  }
}
